package com.example.bootdemo.atomic;

import java.util.Random;
import java.util.concurrent.atomic.LongAccumulator;

public class MonitoringDevice1 implements Runnable {
    private final LongAccumulator maleCount;
    private final String monitoringDeviceNo;

    public MonitoringDevice1(LongAccumulator maleCount, int monitoringDeviceNo) {
        this.maleCount = maleCount;
        this.monitoringDeviceNo = "第" + monitoringDeviceNo + "监控采集处";
    }

    /**
     * 设备运行的处理逻辑
     */
    public void run() {
        while (true) {
            // 监测处理 (监测设备输出1代表男性，0代表女性，其他代表未能识别，此处随机产生监测结果)
            try {
                Thread.sleep(new Random().nextInt(3000));
            } catch (Exception e) {
            }
            int monitoringDeviceOutput = new Random().nextInt(3);

            // 对监测结果进行统计
            switch (monitoringDeviceOutput) {
                case 1:
                    maleCount.accumulate(1);
                    System.out.println("统计结果: maleCount=" + maleCount.get());
                    break;
                default:
                    System.out.println("忽略统计");
                    break;
            }
        }
    }
}
